const reqlib = require('app-root-path').require
const numberUtil = reqlib('/app/util/numberUtil')

module.exports = {
  // 人工理由的平仓，如果修改了移动端也要跟着修改
  pingKongByManualReason () {
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-1') {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-man-1')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-2') {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-man-2')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-3') {
      if (this.analysisInfo15m.ema5W10B20Days >= 1) {
        this.setPingKong('pingKongByReason-man-3')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-4') {
      if (this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-man-4')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-5') {
      if (this.analysisInfo15m.kdjDiff < 0) {
        this.setPingKong('pingKongByReason-man-5')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-6') {
      if (this.analysisInfo15m.macd < 0 && this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-man-6')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-7') {
      if (this.analysisInfo15m.macd < 0 || this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-man-7')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-8') {
      if (this.analysisInfo15m.macd < 0 && this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-man-8')
      }
    }
    if (this.btbItem.buy_sell_reason === 'sellIfOnlyPingByReason-man-9') {
      if (this.analysisInfo15m.macd < 0 || this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-man-9')
      }
    }
  },
  pingKongByReason () {
    if (!this.btbItem.buy_sell_reason) {
      return
    }
    if ([
      'sellIfOnlyPingByReason-1',
      'sellIfOnlyPingByReason-2',
      'sellIfOnlyPingByReason-3',
      'sellIfOnlyPingByReason-3-1'
    ].indexOf(this.btbItem.buy_sell_reason) !== -1) {
      if (
        this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10
      ) {
        this.setPingKong('pingKongByReason-1')
      }
      if (
        this.analysisInfo15m.kdjDiff < 0 &&
        this.analysisInfo15m.netChangeRatioDownDays >= 4 &&
        this.analysisInfo15m.low < this.analysisInfo15m.ema10
      ) {
        this.setPingKong('pingKongByReason-1-1', {
          rate: 0.5
        })
      }
      if (
        this.analysisInfo15m.kdjDiff < 0 &&
        this.analysisInfo15m.macd < 0
      ) {
        this.setPingKong('pingKongByReason-1-2', {
          rate: 0.5
        })
      }
      if (
        (
          this.analysisInfo15m.kdjUpDays >= 9 &&
          this.btbItem.keep_days >= 5
        )
      ) {
        this.setPingKong('pingKongByReason-2')
      }
      if (this.btbItem.buy_sell_reason !== 'sellIfOnlyPingByReason-3') {
        if (
          this.analysisInfo15m.low < this.analysisInfo15m.ema5 &&
          this.analysisInfo15m.netChangeRatio < 0
        ) {
          this.setPingKong('pingKongByReason-3')
        }
        if (
          (this.marketInfo.macd4hGood && this.marketInfo.ema4hGood) &&
          (this.marketInfo.macd1hGood || this.marketInfo.ema1hGood)
        ) {
          if (this.analysisInfo15m.netChangeRatio < 0) {
            this.setPingKong('pingKongByReason-3-1')
          }
        }
        if (
          this.analysisInfo15m.netChangeRatio <= -5 &&
          numberUtil.countDifferenceRate(this.analysisInfo15m.low, this.analysisInfo15m.ema5) < 1 &&
          this.analysisInfo15m.kdjDiff < 0
        ) {
          this.setPingKong('pingKongByReason-3-2', {
            rate: 0.5
          })
        }
      }
      if (this.marketFlagInfo.isMarketLittleGoodUp) {
        if (this.analysisInfo15m.netChangeRatio < 0) {
          this.setPingKong('pingKongByReason-3-2')
        }
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-6'])) {
      if (
        this.analysisInfo15m.ema5W10B20Days >= 1 &&
        this.analysisInfo15m.macdDownDays >= 1
      ) {
        this.setPingKong('pingKongByReason-4-4')
        this.manual = 1
      }
      if (this.analysisInfo15m.macdUpDays >= 2 && this.analysisInfo15m.ema5W10B20Days >= 1) {
        this.setPingKong('pingKongByReason-4-4-1')
        this.manual = 1
      }
      if (this.analysisInfo15m.netChangeRatio < -15) {
        this.setPingKong('pingKongByReason-4-4-2')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-7'])) {
      if (
        this.analysisInfo15m.ema5W10B20Days >= 1 &&
        this.analysisInfo15m.kdjDiff > 0
      ) {
        this.setPingKong('pingKongByReason-4-5')
        this.manual = 1
      }
      if (this.analysisInfo15m.macdUpDays >= 2 && this.analysisInfo15m.ema5W10B20Days >= 1) {
        // 可能要开启第二波上涨
        this.setPingKong('pingKongByReason-4-5-1')
      }
      if (this.analysisInfo15m.netChangeRatio < -15) {
        this.setPingKong('pingKongByReason-4-5-2')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-7-1'])) {
      if (this.analysisInfo15m.macdUpDays >= 2 && this.analysisInfo15m.ema5W10B20Days >= 1) {
        this.setPingKong('pingKongByReason-4-a1-1')
      }
      if (
        this.analysisInfo15m.ema5W10B20Days >= 1 &&
        this.analysisInfo15m.kdjDiff > 0
      ) {
        this.setPingKong('pingKongByReason-4-a1-2')
        this.manual = 1
      }
      if (this.analysisInfo15m.netChangeRatio < -15) {
        this.setPingKong('pingKongByReason-4-a1-3')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-8-0'])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-4-6')
        this.manual = 1
      }
      // if (this.analysisInfo15m.kdjDiff < 0) {
      //   this.setPingKong('pingKongByReason-4-7')
      // }
      if (this.analysisInfo15m.netChangeRatio < -15) {
        this.setPingKong('pingKongByReason-4-8')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-8-1'])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-4-101')
      }
      if (this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-4-102')
      }
    }
    if ([
      'sellIfStepBack-1',
      'sellIfStepBack-2'
    ].indexOf(this.btbItem.buy_sell_reason) !== -1) {
      // 回踩的
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        if (this.marketInfo && this.marketInfo.kdj15mBad) {
          this.setPingKong('pingKongByReason-5-1')
        }
      }
      if (this.symbolAccount.incomeRate <= -4) {
        this.setPingKong('pingKongByReason-5-2')
      }
      if (this.analysisInfo15m.ema5B10B20Days >= 1 && this.analysisInfo15m.isKdjIncrease) {
        this.setPingKong('pingKongByReason-5-3')
      }
      if (this.marketMachine.ifMarketModelLike('[[0,1,0],[-1,-1,1],[0,0,-1],[-1,0,0]]')) {
        this.setPingKong('pingKongByReason-5-4')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-13'])) {
      if (this.analysisInfo15m.close < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-10-1')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-10-2')
      }
      if (this.analysisInfo15m.kdjDiff > 0) {
        this.setPingKong('pingKongByReason-10-3')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-15'])) {
      if (this.analysisInfo15m.kdjDiff > 0) {
        this.setPingKong('pingKongByReason-12-1')
      }
      if (this.analysisInfo15m.isKdjIncrease && this.is4hLastTime) {
        this.setPingKong('pingKongByReason-12-2')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-16'])) {
      if (this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-13-1')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-13-2')
      }
      if (this.analysisInfo15m.close < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-13-3')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-17'])) {
      if (this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-14-1')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-14-2')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-18'])) {
      if (this.analysisInfo15m.kdjDiff > 0) {
        this.setPingKong('pingKongByReason-15-1')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-19'])) {
      if (
        this.analysisInfo15m.ema5W10B20Days >= 1 &&
        this.analysisInfo15m.kdjDiff > 0
      ) {
        this.setPingKong('pingKongByReason-16-1')
        this.manual = 1
      }
      if (this.analysisInfo15m.macdUpDays >= 2 && this.analysisInfo15m.ema5W10B20Days >= 1) {
        this.setPingKong('pingKongByReason-16-2')
        this.manual = 1
      }
      if (this.analysisInfo15m.netChangeRatio < -15) {
        this.setPingKong('pingKongByReason-16-3')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-20'])) {
      if (
        this.analysisInfo15m.ema5W10B20Days >= 1 &&
        (this.analysisInfo15m.macd > 0 || this.analysisInfo15m.kdjDiff > 0)
      ) {
        this.setPingKong('pingKongByReason-17-1')
      }
      if (
        this.analysisInfo15m.ema5W10A20Days >= 1 &&
        this.btbItem.keep_days >= 9
      ) {
        this.setPingKong('pingKongByReason-17-2')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-21'])) {
      if (
        this.analysisInfo15m.kdjDiff < 0 &&
        this.analysisInfo15m.low < this.analysisInfo15m.ema5 &&
        this.analysisInfo15m.netChangeRatio < 0
      ) {
        this.setPingKong('pingKongByReason-18-1')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-21-1'])) {
      if (
        this.analysisInfo15m.macd < 0 ||
        this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10
      ) {
        this.setPingKong('pingKongByReason-18-1-1')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-22'])) {
      if (
        this.analysisInfo15m.low < this.analysisInfo15m.ema20
      ) {
        this.setPingKong('pingKongByReason-18-3')
      }
      if (this.btbItem.keep_days >= 6) {
        this.setPingKong('pingKongByReason-18-2')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-23'])) {
      if (this.analysisInfo15m.ema5B10B20Days >= 1) {
        this.setPingKong('pingKongByReason-19-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-24'])) {
      if (
        this.analysisInfo15m.ema5B10B20Days >= 1 &&
        (this.analysisInfo15m.kdjDiff > 0 || this.analysisInfo15m.isMacdIncrease)
      ) {
        this.setPingKong('pingKongByReason-20-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-25'])) {
      if (
        this.analysisInfo15m.ema5B10B20Days >= 1 &&
        (this.analysisInfo15m.kdjDiff > 0 || this.analysisInfo15m.isMacdIncrease)
      ) {
        this.setPingKong('pingKongByReason-21-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-26'])) {
      if (
        this.analysisInfo1h.ema5B10B20Days >= 1 &&
        (this.analysisInfo1h.kdjDiff > 0 || this.analysisInfo1h.isMacdIncrease) &&
        this.tradeTime15mCycle === 4
      ) {
        this.setPingKong('pingKongByReason-22-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-27'])) {
      if (
        this.analysisInfo15m.ema5B20Days >= 1 &&
        (this.analysisInfo15m.macd < 0)
      ) {
        this.setPingKong('pingKongByReason-23-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-1',
      'sellIfOnlyPingByReason-p-7',
      'sellIfOnlyPingByReason-28',
      'sellIfOnlyPingByReason-p-9'
    ])) {
      if (this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-25-0')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-25-1')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-2'
    ])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10 && this.analysisInfo15m.isKdjIncrease) {
        this.setPingKong('pingKongByReason-26-0')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-26-1')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-3'
    ])) {
      if (this.analysisInfo15m.kdjDiff < 0) {
        this.setPingKong('pingKongByReason-26-1-1')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-4',
      'sellIfOnlyPingByReason-p-4-1'
    ])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10 && this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-27-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-5',
      'sellIfOnlyPingByReason-p-6'
    ])) {
      if (this.analysisInfo15m.ema5B10B20Days >= 1 && this.analysisInfo15m.macd < 0 && this.analysisInfo15m.isKdjIncrease) {
        this.setPingKong('pingKongByReason-28-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-8'
    ])) {
      if (this.analysisInfo15m.ema5W10B20Days >= 1 && this.analysisInfo15m.kdjDiff > 0) {
        this.setPingKong('pingKongByReason-29-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-b-1'
    ])) {
      if (this.analysisInfo15m.ema5W10B20Days >= 1) {
        this.setPingKong('pingKongByReason-30-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-10'
    ])) {
      if (this.analysisInfo15m.ema5B10B20Days >= 1) {
        this.setPingKong('pingKongByReason-31-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-p-11'
    ])) {
      if (this.analysisInfo15m.ema5B10B20Days >= 1 && this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-32-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-0001'])) {
      if (this.analysisInfo15m.ema5W10B20Days >= 1 && this.analysisInfo15m.kdjDiff > 0) {
        this.setPingKong('pingKongByReason-33-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-0002'])) {
      if (this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-34-0')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10) {
        this.setPingKong('pingKongByReason-34-1')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-0003'])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema10 && this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-35-0')
      }
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20) {
        this.setPingKong('pingKongByReason-35-1')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-0004'])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20 && this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-36-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-0005', 'sellIfOnlyPingByReason-0006', 'sellIfOnlyPingByReason-0009'])) {
      if (this.analysisInfo15m.ema5 < this.analysisInfo15m.ema20 && this.analysisInfo15m.macd < 0) {
        this.setPingKong('pingKongByReason-37-0')
      }
    }
    if (this.isInBuySellReason(['sellIfOnlyPingByReason-0007'])) {
      if (this.analysisInfo15m.ema5B10B20Days >= 1) {
        this.setPingKong('pingKongByReason-38-0')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-0008',
      'sellIfOnlyPingByReason-0010'
    ])) {
      if (this.analysisInfo1h.ema5 < this.analysisInfo1h.ema20 && this.tradeTime15mCycle === 4) {
        this.setPingKong('pingKongByReason-39-0')
      }
      if (this.analysisInfo1h.kdjDiff > 0 && this.tradeTime15mCycle === 4) {
        this.setPingKong('pingKongByReason-39-1')
      }
    }
    if (this.isInBuySellReason([
      'sellIfOnlyPingByReason-0011'
    ])) {
      if (this.analysisInfo15m.kdjDiff > 0) {
        this.setPingKong('pingKongByReason-40-0')
      }
      if (
        this.analysisInfo15m.isKdjIncrease || this.analysisInfo15m.isMacdIncrease
      ) {
        if (this.symbolAccount.incomeRate < 0) {
          this.setPingKong('pingKongByReason-40-1')
        }
      }
    }
  },
  pingKongByConfig () {
    // 平仓
    if (this.btbItem.ping_next_k && this.hasKongPosition) {
      this.setPingKong('pingKongByConfig-1')
    }
    if (this.btbItem.ping_next_1hk && this.hasKongPosition && this.tradeTime15mCycle === 4) {
      this.setPingKong('pingKongByConfig-2')
    }
  }
}
